<?php
// 登录
class LoginAction extends Action {
	
	private $cLogs = null;
	
	function __construct(){
		
		import('@.ORG.Logs');
		
		$this->cLogs 		= new Logs();
	}
	
    public function index(){
   
    	import("@.ORG.Admin");
    	$cAdmin = new Admin();

    	$this->assign(
    		array(
    			'APP_NAME'=>APP_NAME
    		)
    	);
    	$this->display();
    }
    
    public function login_do(){
    	
    	//用户名和密码
    	$name 	= $this->_post('name'	,'','');
        $passwd	= $this->_post('passwd'	,'','');

        //验证码
        //$verify_code	= $this->_post('verify_code','',0);

        //先判断验证码
        //import("@.ORG.VerifyCode");
        //$cVerifyCode = new VerifyCode();

        //$verify_result = $cVerifyCode->check_session_verify($verify_code);
        //if(!$verify_result){
        //    json_error('验证码错误');
        //}

		import("@.ORG.Admin");
		$cAdmin = new Admin();

		//登录
		$login_result = $cAdmin->login($name,$passwd);

		if($login_result){
			$this->cLogs->add('Login:login_do','管理员登录成功:'.$name);	
			json_success('登录成功');
		}else{
			$this->cLogs->add('Login:login_do','管理员登录失败:'.$name);
			json_error('登录失败');
		}
    }

    /**
     * 发送短信验证码
     */
    public function send_sms_verify_code(){

        //用户名和密码
        $mobile_phone 	= $this->_post('mobile_phone'	,'','');

        import("@.ORG.Admin");
        $cAdmin = new Admin();

        $admin = $cAdmin->get_by_mobile_phone($mobile_phone);

        if($admin){

            //验证码
            import("@.ORG.VerifyCode");
            $cVerifyCode = new VerifyCode();

            $code = $cVerifyCode->create_session_verify();

            //发送短信
            import("@.ORG.Sms");
            $cSms = new Sms();
            $msg = $code;
            $mobile_phone = $admin['mobile_phone'];
            $result = $cSms->send($mobile_phone,$msg);

            //添加到数据库
            import("@.ORG.SmsLog");
            $cSmsLog = new SmsLog();


            $return_value   = print_r($result,true);
            $admin_id       = $admin['id'];
            $data = array(
                'mobile'        => $mobile_phone,
                'msg'           => $msg,
                'return_value'  => $return_value,
                'admin_id'      => $admin_id
            );
            $cSmsLog->add($data);

            json_success('验证短信已发送成功，请注意查收');

        }else{
            json_error('手机号不存在，请检查');
        }
    }

    public function login_status(){
    	//session_destroy();
    	
    	import("@.ORG.Admin");
		$cAdmin = new Admin();
		
		//前缀
		$prefix = Admin::SESSION_PREFIX;
		
    	if(session('?'.$prefix.'id')){
    		
    	}
    }
}
?>